﻿using AhCha.Fortunate.Entity.DaMeng;
using AhCha.Fortunate.IService.DaMeng;
using AhCha.Fortunate.ModelsDto;
using AhCha.Fortunate.ModelsDto.DaMeng.ApiRequestLog;
using AhCha.Fortunate.ModelsDto.MSSQL.SysRoleDto;
using AhCha.Fortunate.Repositories.SqlSugar;

using Mapster;

using SqlSugar;

namespace AhCha.Fortunate.Service.DaMeng
{
    public class ApiRequestLogService : BaseServices<ApiRequestLog>, IApiRequestLogService
    {
        public ApiRequestLogService(SqlSugarRepository<ApiRequestLog> TEntityRep) : base(TEntityRep)
        {
        }

        public async Task AddApiRequestLog(AddApiRequestLogInpput input)
        {
            var entity = input.Adapt<ApiRequestLog>();
            await _TEntityRep.InsertAsync(entity);
        }

        public async Task<SqlSugarPagedList<ApiRequestLogOutput>> GetApiRequestLogPage(QueryApiRequestLogInpput input)
        {
            var query = await _TEntityRep.AsQueryable()
             .WhereIF(!string.IsNullOrWhiteSpace(input.Name), x => SqlFunc.Contains(x.ControllerName, input.Name) || SqlFunc.Contains(x.ActionName, input.Name))
             .WhereIF(input.StartQueryTime != null && input.EndQueryTime != null, x => SqlFunc.Between(x.CreateTime.Value, input.StartQueryTime.Value, input.EndQueryTime.Value))
             .Select<ApiRequestLogOutput>()
             .ToPagedListAsync(input.PageIndex, input.PageSize);
            return query;
        }
    }
}
